package com.ruoyi.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.UserExam;
import com.ruoyi.business.domain.UserExamQuestion;
import com.ruoyi.business.domain.bo.UserExamQuestionBo;
import com.ruoyi.business.mapper.UserExamMapper;
import com.ruoyi.business.mapper.UserExamQuestionMapper;
import com.ruoyi.business.service.IUserExamQuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 最爱喝兽奶
 * @create 2022/8/24
 **/
@SuppressWarnings("ALL")
@Service
public class UserExamQuestionServiceImpl extends ServiceImpl<UserExamQuestionMapper, UserExamQuestion> implements IUserExamQuestionService {

    @Autowired
    private UserExamMapper userExamMapper;

    @Override
    @Transactional
    public void submitExamSubjectiveQuestionEvaluation(List<UserExamQuestionBo> list) {
        // 主观题的总得分
        Integer totalScore = 0;
        String userId = null;
        String examId = null;
        String examNum = null;
        for (UserExamQuestionBo userExamQuestionBo : list) {
            baseMapper.updateScoreByQuestion(userExamQuestionBo);
            totalScore += userExamQuestionBo.getScore();
            if (null == userId) {
                userId = userExamQuestionBo.getUserId();
            }
            if (null == examId) {
                examId = userExamQuestionBo.getExamId();
            }
            if (null == examNum) {
                examNum = String.valueOf(userExamQuestionBo.getExamNum());
            }
        }
        // 修改用户考试的总得分
        QueryWrapper<UserExam> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("exam_id", examId);
        wrapper.eq("num", examNum);
        UserExam userExam = userExamMapper.selectOne(wrapper);
        if (null == userExam) {
            throw new RuntimeException("请输入正确的考试用户");
        }
        userExam.setSubjectiveScore(totalScore);
        userExam.setScore(userExam.getSubjectiveScore() + userExam.getObjectiveScore());
        userExamMapper.updateById(userExam);
    }
}
